import { map } from 'lodash-es'

import { http, randomUUid } from '@/utils'

export type GetNodeTypeFromValue<T extends string> = T extends `${infer Type}Id`
  ? Type
  : never

/* 省份列表 */
export function getProvinceListApi(query?: any): Promise<any> {
  return http.get({
    url: '/paas/paaspositionprovice/list',
    query: { size: 100, ...query }
  })
}

/* 市/自治区/自治州 列表 */
export function getCityListApi(query?: any): Promise<any> {
  return http.get({
    url: '/paas/paaspositioncity/list',
    method: 'GET',
    query: { size: 100, ...query }
  })
}

/* 区县列表接口 */
export function getCountyListApi(query?: any): Promise<any> {
  return http.get({
    url: '/paas/paaspositioncounty/list',
    method: 'GET',
    query: { size: 100, ...query }
  })
}

/* 乡镇列表接口 */
export function getTownListApi(query?: any): Promise<any> {
  return http.get({
    url: '/paas/paaspositiontown/list',
    method: 'GET',
    query: { size: 100, ...query }
  })
}

/* 村/社区列表接口 */
export function getVillageListApi(query: any): Promise<any> {
  return http.get({
    url: '/paas/paaspositionvillage/list',
    method: 'GET',
    query: { size: 100, ...query }
  })
}

export function getTreeByCountyIdApi(query?: any): Promise<any> {
  return http.get({
    url: '/paas/paaspositioncounty/getTreeByCounty',
    method: 'GET',
    query
  })
}

// 查省详情
export function getProvinceDetailApi(query?: any): Promise<any> {
  return http.get({
    url: '/paas/paaspositionprovice/detail',
    method: 'GET',
    query
  })
}

// 查市详情
export function getCityDetailApi(query: any): Promise<any> {
  return http.get({
    url: '/paas/paaspositioncity/detail',
    method: 'GET',
    query
  })
}

// 查县详情
export function getCountyDetailApi(query: any): Promise<any> {
  return http.get({
    url: '/paas/paaspositioncounty/detail',
    method: 'GET',
    query
  })
}

// 过滤地址结果
export const filterAddress = <
  T extends Record<string, any>[],
  K extends keyof T[number] & string
>(
  records: T,
  labelField: K,
  valueField: K,
  type: GetNodeTypeFromValue<K>
) =>
  map(records, (o) => ({
    ...o,
    label: o[labelField],
    key: randomUUid(),
    value: o[valueField],
    type: type
  }))

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
export const treeFilterResult = <
  T extends Record<string, any>[],
  K extends keyof T[number] & string
>(
  records: T,
  labelField: K,
  valueField: K,
  type: string
) =>
  map(records, (o) => ({
    ...o,
    label: o[labelField],
    key: randomUUid(),
    value: o[valueField],
    type: type,
    isLeaf: !o.children?.length,
    children: o.children?.length
      ? treeFilterResult(o.children, labelField, valueField, type)
      : undefined
  }))

export async function addressAsyncLoadData(node?: any) {
  if (!node) {
    const { records } = await getProvinceListApi()
    return filterAddress(records, 'provinceName', 'provinceId', 'province')
  }

  const { type, value, countyId } = node
  if (type === 'province') {
    const { records } = await getCityListApi({ provinceId: value })
    return filterAddress(records, 'cityName', 'cityId', 'city')
  }
  if (type === 'city') {
    const { records } = await getCountyListApi({ cityId: value })
    return filterAddress(records, 'countyName', 'countyId', 'county')
  }
  if (countyId) {
    return treeFilterResult(
      await getTreeByCountyIdApi({ countyId }),
      'value',
      'id',
      'last1'
    )
  }

  return []
}
